// pages/myhistory/myhistory.js
let db = wx.cloud.database()
let subject = db.collection("subject")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        subjectData : [],
        isDone : [],
        title : '',
        keyword : '',
        IsOK : false, 
    },
    getSubData(keyword,name){
        // console.log(keyword,name);
        wx.showLoading({
          title: '正在加载',
        })
        this.setData({
            title : name,
            keyword : keyword
        })
        if(keyword=='collect'){
            this.getCollect();
        }else if(keyword=='history'){
            this.getHistory();
        }
    },
    getCollect(){
        let that = this;
    subject.count()
      .then(
        async res => {
        //   console.log(res);
          let total = res.total; 
          let len = Math.ceil(total / 20);
          for (let i = 0; i < len; i++) { 
            await subject.skip(i * 20).limit(20).where({
                collect : true
            })
              .get()
              .then(
                async res => {
                  let new_data = res.data;
                  let old_data = that.data.subjectData;
                  let newarr = that.data.isDone;
                  newarr.push(0);
                //   console.log(new_data,'1.0');
                //   console.log(old_data,'2.0');
                  that.setData({
                     subjectData: old_data.concat(new_data),
                     isDone : newarr,
                  })
                  if(that.data.isDone.length >= len){
                    //   console.log("完成")
                    that.setData({
                      IsOK : true,
                    })
                    wx.hideLoading()
                  }
                }
              )
          }
        }
      )
    },
    getHistory(){
        let that = this;
        subject.count()
          .then(
            async res => {
            //   console.log(res);
              let total = res.total; 
              let len = Math.ceil(total / 20);
              for (let i = 0; i < len; i++) { 
                await subject.skip(i * 20).limit(20).where({
                    record : true
                })
                  .get()
                  .then(
                    async res => {
                      let new_data = res.data;
                      let old_data = that.data.subjectData;
                      let newarr = that.data.isDone;
                      newarr.push(0);
                    //   console.log(new_data,'1.0');
                    //   console.log(old_data,'2.0');
                      that.setData({
                         subjectData: old_data.concat(new_data),
                         isDone : newarr,
                      })
                      if(that.data.isDone.length >= len){
                        //   console.log("完成")
                        that.setData({
                          IsOK : true,
                        })
                        wx.hideLoading()
                      }
                    }
                  )
              }
            }
          )
    },
    delete(e){
      // console.log(e.currentTarget.dataset.i);
      let index = e.currentTarget.dataset.i
      let newarr = this.data.subjectData;
      let _id = newarr[index]._id;
      let that = this;
      newarr.splice(index,1);
      if(this.data.keyword == 'collect'){
          // console.log(111)
          subject.doc(_id).update({
            data : {
              collect : false
            }
          }).then(
            res =>{
              // console.log(res)
              if(res.errMsg = "document.update:ok"){
                wx.showToast({
                  title: '删除成功',
                })
                that.setData({
                  subjectData : newarr
                })
              }
            }
          )
      }else {
        // console.log(222)
        subject.doc(_id).update({
          data : {
            record : false
          }
        }).then(
          res =>{
            // console.log(res)
            if(res.errMsg = "document.update:ok"){
              wx.showToast({
                title: '删除成功',
              })
              that.setData({
                subjectData : newarr
              })
            }
          }
        )
      }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options);
        this.getSubData(options.keyword,options.name);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})